package cn.lzg.common.core.codec;

import cn.lzg.common.core.model.Response;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class ResponseEncoder extends MessageToByteEncoder<Response>{

	@Override
	protected void encode(ChannelHandlerContext ctx, Response msg, ByteBuf out) throws Exception {
		out.writeInt(Constants.HEADER_FLAG);
		out.writeShort(msg.getModule());
		out.writeShort(msg.getCmd());
		
		int len_data = msg.getData()==null?0:msg.getData().length;
		
		if(len_data <=0) {
			
		}else {
			out.writeInt(len_data);
			out.writeBytes(msg.getData());
		}
		
	}

}
